package com.xhtt.modules.msg.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.modules.msg.dao.MsgUserMapper;
import com.xhtt.modules.msg.entity.MsgInfo;
import com.xhtt.modules.msg.entity.MsgUser;
import com.xhtt.modules.msg.service.MsgInfoService;
import com.xhtt.modules.msg.service.MsgUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 消息表 服务实现类
 * </p>
 *
 * @author feipc
 * @since 2019-03-27
 */
@Service
public class MsgUserServiceImpl extends ServiceImpl<MsgUserMapper, MsgUser> implements MsgUserService {

    @Autowired
    private MsgInfoService msgInfoService;

    @Override
    @Transactional
    public void insertBatch(MsgInfo msgInfo, List<Integer> userIds) {
        boolean r = msgInfoService.save(msgInfo);
        if (r) {
            List<MsgUser> msgUsers = new ArrayList<>();
            userIds.forEach(userId -> {
                MsgUser msgUser = new MsgUser();
                msgUser.setMsgId(msgInfo.getId());
                msgUser.setUserId(userId);
                msgUsers.add(msgUser);
            });
            this.saveBatch(msgUsers);
        }
    }

    @Override
    public void insertBatch(List<MsgInfo> msgInfoList, List<Integer> userIds) {

        msgInfoList.forEach(msgInfo -> insertBatch(msgInfo, userIds));

    }
}
